function BCAinstallControls () {
    // Check missiles
    if (localStorage.getItem ("po_bca_check_all_missiles") == "true") {
        checkAllMissiles ();
    }
    
    // Reverse the attack rounds options so that 20 is on the top (pre-selected)
    var option;
    var select;
    var rtext;
    var rtext2;
    select = document.getElementsByTagName('select');
    if (select[0].hasAttribute('style')) { option = select[1].getElementsByTagName('option') }
    else { option = select[0].getElementsByTagName('option'); }

    for(var i = 0; i < option.length; i++) {
	option[i].setAttribute("value", option.length-i);
    }
    for(var i = 0; i < (option.length/2); i++) {
	rtext = option[i].textContent;
	rtext2 = option[option.length-1-i].textContent;
	option[option.length-1-i].innerHTML = rtext;
	option[i].innerHTML = rtext2;
    }
    
    BCAinstallBotsDrugs ();
}

function BCAinstallBotsDrugs () {
    var fnt = null;
    var fnts = document.getElementsByTagName ("font");
    for (var i=0; i<fnts.length; i++) {
        if (fnts[i].textContent.indexOf ("Armor points:") == 0) {
            fnt = fnts[i];
            break;
        }
    }

    if (fnt) {
        var current = Number (fnt.textContent.split (": ")[1].split(" ")[0]);
        var armorS = localStorage.getItem ("po_bca_armor_strength");
        var armorP = localStorage.getItem ("po_bca_armor_points");
        if (!armorS || !armorP) return;
        armorS = Number (armorS);
        armorP = Number (armorP);
        var steps = POgetBotsToRepair (armorS, current, armorP);
        fnt.innerHTML += "&nbsp;<a id='bca_repair_armor_combat' amount='"+steps+"' href=\"javascript:BCAtakeBots;void(0)\">Press 4 to take "+steps+" robots</a>";
        
        var tbl = fnt.parentNode.parentNode.parentNode;
        var tr = document.createElement ("tr");
        var toTake = Number (localStorage.getItem("po_bca_default_drugs"));
        tbl.appendChild (tr);
        tr.innerHTML = "<td><a id='bca_take_drugs' amount='"+toTake+"' href=\"javascript:BCAtakeDrugs;void(0)\">Press 5 to take "+toTake+" drugs</a></td>";
    }    
}

//window.onload = setTimeout (function () {
    BCAinstallControls ();
//}, 10);

// //////////////////////////////////////////////////////////
// Hotkeys
// //////////////////////////////////////////////////////////
window.addEventListener("keypress",BCAkeyboardAction,true);

function BCAkeyboardAction (e) {
    if(window.name == '' || e.ctrlKey) return;
    if (e.target.nodeName.toUpperCase() == 'INPUT') return;
    var keynum = e.which;
    var key = String.fromCharCode(keynum).toLowerCase();
    
    if (key == "3") BCAfightTwenty ();
    else if (key == "1") BCAretreat ();
    else if (key == "2") BCArefreshScreen ();
    else if (key == "4") BCAtakeBots (-1);
    else if (key == "5") BCAtakeDrugs (-1);
}

function BCAfightTwenty () {
    var btn = BCAgetBtn ("button5");
    if (!btn) {
        btn = BCAgetBtn ("ok");
    }
    btn.click ();
}

function BCAretreat () {
    window.location = "main.php";
}

function BCArefreshScreen () {
    window.location = document.URL;
}

function BCAtakeBots () {
    var amount = Number(document.getElementById ("bca_repair_armor_combat").getAttribute ("amount"));
    var avail = Number(localStorage.getItem ("bca_bots_onboard"));
    localStorage.setItem ("bca_bots_onboard", avail - amount);
    POuseResource (8, amount, BCArefreshScreen);
}

function BCAtakeDrugs () {
    var amount = Number(document.getElementById ("bca_take_drugs").getAttribute ("amount"));
    var avail = Number(localStorage.getItem ("bca_drugs_onboard"));
    localStorage.setItem ("bca_drugs_onboard", avail - amount);
    POuseResource (51, amount, BCArefreshScreen);
}


// //////////////////////////////////////////////////////////
// End: Hotkeys
// //////////////////////////////////////////////////////////

function BCAgetBtn (name) {
    var bts = document.getElementsByTagName ("input");
    for (i=0; i<bts.length; i++) {
        if (bts[i].getAttribute("name") == name) return bts[i];
    }
    return null;
}